package com.cyan.note.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.sql.DataSource;
import java.sql.Connection;
import java.util.HashMap;
import java.util.Map;

/**
 * 简单测试控制器
 * 
 * @author ZhangzhenYu
 * @since 2025-01-13
 */
@RestController
@RequestMapping("/test")
@RequiredArgsConstructor
public class SimpleTestController {

    private final DataSource dataSource;

    /**
     * 健康检查
     */
    @GetMapping("/health")
    public Map<String, Object> healthCheck() {
        Map<String, Object> result = new HashMap<>();
        result.put("status", "success");
        result.put("message", "Cyan-Note 后端服务运行正常");
        result.put("timestamp", System.currentTimeMillis());
        result.put("version", "1.0.0");
        return result;
    }

    /**
     * 项目信息
     */
    @GetMapping("/info")
    public Map<String, Object> getInfo() {
        Map<String, Object> result = new HashMap<>();
        result.put("projectName", "Cyan-Note 陪玩订单管理系统");
        result.put("author", "ZhangzhenYu");
        result.put("version", "1.0.0");
        result.put("springBootVersion", "3.2.1");
        result.put("javaVersion", System.getProperty("java.version"));
        result.put("status", "running");
        return result;
    }

    /**
     * 数据库连接测试
     */
    @GetMapping("/database")
    public Map<String, Object> testDatabase() {
        Map<String, Object> result = new HashMap<>();
        
        try {
            Connection connection = dataSource.getConnection();
            String url = connection.getMetaData().getURL();
            String databaseName = connection.getCatalog();
            connection.close();
            
            result.put("status", "success");
            result.put("message", "数据库连接正常");
            result.put("databaseUrl", url);
            result.put("databaseName", databaseName);
            result.put("connectionType", dataSource.getClass().getSimpleName());
            
        } catch (Exception e) {
            result.put("status", "error");
            result.put("message", "数据库连接失败: " + e.getMessage());
            result.put("error", e.getClass().getSimpleName());
        }
        
        return result;
    }
}